package org.batatasframework.support.database.sql;

import java.util.ArrayList;
import java.util.List;

import org.batatasframework.support.database.metadatas.TableMetadata;

public class TableRuntimeTemporary extends TableRuntimeBase{

	private String sqlDrop;
	private String sqlSelect;
	private List<QueryParameter> sqlParamenters = new ArrayList<>();
	
	public TableRuntimeTemporary(TableMetadata tableMetadata, String alias) {
		super(tableMetadata, alias);
	}

	public TableRuntimeTemporary(TableMetadata tableMetadata) {
		super(tableMetadata, "_T");
	}

	public void finish() throws Exception{
		String ttName = getTableName();
		StringBuilder ttSql = new StringBuilder("select * from ").append(ttName).append(" ").append(getAlias());
		String whereClause = getWhereClause(sqlParamenters);
		if(!whereClause.isEmpty()){
			ttSql.append(" where ").append(whereClause);
		}
		
		this.sqlSelect = ttSql.toString();
		this.sqlDrop = "DROP TEMPORARY TABLE IF EXISTS " + ttName + ";";
	}
	
	public String getSqlDrop() {
		return sqlDrop;
	}
	
	public List<QueryParameter> getSqlParamenters() {
		return sqlParamenters;
	}
	
	public String getSqlSelect() {
		return sqlSelect;
	}
}
